package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.*;
import cn.topevery.garden.entity.po.DingtalkDuty;
import cn.topevery.garden.entity.po.DingtalkDutyTimes;
import cn.topevery.garden.entity.query.DingtalkDutyQuery;
import cn.topevery.garden.entity.query.DingtalkDutyTimesQuery;
import cn.topevery.garden.entity.query.DingtalkUserAttendanceResultQuery;
import cn.topevery.garden.entity.vo.*;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 钉钉值班
 *
 * @author code-generate
 * @date 2021-08-30
 */
@Api(tags = "钉钉值班接口")
@RestController
@RequestMapping("/dingTalk")
public class DingTalkController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    private DingtalkDutyLogic dingtalkDutyLogic;

    @Autowired
    private DingtalkDutyTimesLogic dingtalkDutyTimesLogic;

    @Autowired
    private DingtalkAttendanceGroupsLogic dingtalkAttendanceGroupsLogic;

    @Autowired
    private DingtalkAttendanceGroupsWorkDayRelationLogic dingtalkAttendanceGroupsWorkDayRelationLogic;

    @Autowired
    private DingtalkAttendanceScheduleLogic dingtalkAttendanceScheduleLogic;

    @Autowired
    private DingtalkUserRelationLogic dingtalkUserRelationLogic;

    @Autowired
    DingtalkUserAttendanceResultLogic dingtalkUserAttendanceResultLogic;


    /**
     * 获取分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取页列表")
    @PostMapping("/ResultlistPage")
    public JsonResult<DataPager<DingtalkUserAttendanceResultVO>> listPage(@RequestBody DingtalkUserAttendanceResultQuery query) {
        return JsonResult.ok(dingtalkUserAttendanceResultLogic.listPage(query));
    }

    /**
     * 获取分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取页列表")
    @PostMapping("/DutylistPage")
    public JsonResult<DataPager<DingtalkDutyVO>> listPage(@RequestBody DingtalkDutyQuery query) {
        return JsonResult.ok(dingtalkDutyLogic.listPage(query));
    }

    /**
     * 获取分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取页列表")
    @PostMapping("/DutyTimeslistPage")
    public JsonResult<DataPager<DingtalkDutyTimesVO>> listPage(@RequestBody DingtalkDutyTimesQuery query) {
        return JsonResult.ok(dingtalkDutyTimesLogic.listPage(query));
    }

    /**
     * 分页获取所有排班人员信息
     * @param
     * @return
     */
    @ApiOperation(value = "分页获取所有排班人员信息")
    @GetMapping("/DutyEmployeeListPage")
    public JsonResult<DataPager<DingtalkWorkScheduleUserVO>> findAllUserPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                                                      @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                      @RequestParam(value = "name", defaultValue = "", required = false) String name) {
        return JsonResult.ok(dingtalkAttendanceScheduleLogic.findAllUserPage(page, pageSize, name));
    }

    @ApiOperation(value = "获取所有人员指定月份的排班信息")
    @GetMapping("/DutySchedule")
    public JsonResult<List<DingtalkWorkScheduleVO>> selectScheduleByDate(@RequestParam(value = "date") String date) {
        return JsonResult.ok(dingtalkAttendanceScheduleLogic.selectScheduleByDate(date));
    }

    @ApiOperation(value = "获取指定排班信息")
    @GetMapping("/DutyScheduleTimes")
    public JsonResult<List<DingtalkDutyTimesMessageVO>> selectDutyTimesById(@RequestParam(value = "shiftId") Long shiftId) {
        return JsonResult.ok(dingtalkDutyTimesLogic.selectDutyTimesById(shiftId));
    }

    @ApiOperation(value = "获取所有的考勤组")
    @GetMapping("/DutyAttendanceGroups")
    public JsonResult<List<Map<String, Object>>> selectAllGroups() {
        return JsonResult.ok(dingtalkAttendanceGroupsLogic.selectAllGroups());
    }

    @ApiOperation(value = "获取所有的班次")
    @GetMapping("/DutyScheduleDuties")
    public JsonResult<List<Map<String, Object>>> selectAllDuty() {
        return JsonResult.ok(dingtalkDutyLogic.selectAllDuty());
    }

    @ApiOperation(value = "新增班次信息")
    @PostMapping("/addDuty")
    public JsonResult addDuty(@RequestBody DingtalkDutyVO vo) {
        DingtalkDuty dingtalkDuty=mapper.map(vo,DingtalkDuty.class);
        dingtalkDutyLogic.saveOrUpdate(getUser().id.toString(),dingtalkDuty);
        if(dingtalkDuty.id>0){
            vo.getDutyTimes().forEach(f->{
                DingtalkDutyTimes dingtalkDutyTimes=mapper.map(f,DingtalkDutyTimes.class);
                dingtalkDutyTimes.dutyDataId=dingtalkDuty.dataId;
                dingtalkDutyTimesLogic.saveOrUpdate(getUser().id.toString(),dingtalkDutyTimes);
            });
        }
        return JsonResult.ok();
    }

    @ApiOperation(value = "删除班次信息")
    @PostMapping("/delDuty")
    public JsonResult delDuty(@RequestBody Integer[] ids) {
        dingtalkDutyLogic.deleteBatchById(getUser().id.toString(),ids);
        return JsonResult.ok(true);
    }

}
